﻿// GameApp.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "GameApp.h"
#include "MiniOne.h"
#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 
std::string szWindowClass = "MiniOneEngine";            // the main window class name
std::string szTitle = "GameApp";
INT32 WINDOW_WIDTH = 800;
INT32 WINDOW_HEIGHT = 600;


LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

// 此代码模块中包含的函数的前向声明:
//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance, DWORD nIcon, DWORD nStyle)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style = nStyle;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(nIcon));
	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GAMEAPP);
	wcex.lpszClassName = szWindowClass.c_str();
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(nIcon));

	return RegisterClassEx(&wcex);
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

	RECT rc;
	SetRect(&rc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
	AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW&(~WS_MAXIMIZEBOX)&(~WS_SIZEBOX), TRUE);
	WINDOW_WIDTH = rc.right - rc.left;
	WINDOW_HEIGHT = rc.bottom - rc.top;
	MyRegisterClass(hInstance, IDI_GAMEAPP, CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW);
	HWND hWnd = CreateWindow(szWindowClass.c_str(), szTitle.c_str(), WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX),
		(GetSystemMetrics(SM_CXSCREEN) - WINDOW_WIDTH) / 2, (GetSystemMetrics(SM_CYSCREEN) - WINDOW_HEIGHT) / 2,
		(rc.right - rc.left), (rc.bottom - rc.top), nullptr, nullptr, hInstance, nullptr);
	if (!hWnd)
	{
		return FALSE;
	}
	ShowWindow(hWnd, TRUE);
	UpdateWindow(hWnd);

	mini::CEngine* pEngine = mini::createEngine(hWnd);
	if (pEngine != nullptr)
	{
		pEngine->onStart();
		pEngine->stopRender();
		mini::CScene* pWorldScne = pEngine->loadScene("demo");
		pEngine->recoverRender();

		pEngine->onRun();
		pEngine->destory();
	}
	return (int)0;
}




//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
